package com.droidexchange.activity;

import java.util.List;
import net.sf.stackwrap4j.entities.Answer;
import net.sf.stackwrap4j.entities.Question;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Debug;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ListView;
import com.droidexchange.R;
import com.droidexchange.adapter.AnswersAdapter;
import com.droidexchange.adapter.QuestionsAdapter;
import com.droidexchange.custom.AnswerTabPanel;
import com.droidexchange.main.DeMajorPost;
import com.droidexchange.main.DeQuestionsItem;
import com.droidexchange.util.Util;

public class AnswersActivity extends Activity
{
	private ListView	listView;
	private Question	question;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		int qid = getIntent().getIntExtra("qid", -1);
		Util.log("Question in AnsActivity: " + qid);
		if (qid == -1)
			finish();

		setContentView(R.layout.answer_list);
		listView = (ListView) findViewById(R.id.listview);

		try
		{
			new GetAnswersTask().execute(new Integer[] { qid });
		}
		catch (Exception e)
		{
			e.printStackTrace();
			finish();
		}
	}

	private class GetAnswersTask extends AsyncTask<Integer, Void, AnswersAdapter>
	{
		private RotateAnimation	rotate;
		private View			pendingView;

		@Override
		protected void onPreExecute()
		{
			setPendingView();
			FrameLayout frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
			frameLayout.addView(pendingView);
		}

		@Override
		protected AnswersAdapter doInBackground(Integer... qids)
		{
			try
			{
				int qid = qids[0];
				question = Util.sw.getQuestionById(qid);
				if (question == null)
					finish();

				List<Answer> answers = question.getAnswers();
				AnswerTabPanel answerTabPanel = (AnswerTabPanel) findViewById(R.id.atabp_holder);

				// Cache the UI elements for displaying answers to see if
				// it is faster while executing. This is at the cost of
				// waiting up-front longer. A better balance should be used
				AnswersAdapter answersAdapter = new AnswersAdapter(AnswersActivity.this, R.layout.answer_list);
				answersAdapter.setFloatingPanel(answerTabPanel);
				
				DeMajorPost.setContext(AnswersActivity.this);
				answersAdapter.add(new DeMajorPost(question));
				for (int i = 0; i < answers.size(); i++)
					answersAdapter.add(new DeMajorPost(answers.get(i)));

				return answersAdapter;
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}

			return null;
		}

		protected void onPostExecute(AnswersAdapter answersAdapter)
		{
			View qTabView = LayoutInflater.from(AnswersActivity.this).inflate(R.layout.question_tab, null);
			DeQuestionsItem post = new DeQuestionsItem(question);

			qTabView = QuestionsAdapter.getView(AnswersActivity.this, qTabView, post);
			listView.addHeaderView(qTabView);

			listView.setAdapter(answersAdapter);
			listView.setOnScrollListener(answersAdapter);

			rotate = null;
			pendingView.setVisibility(View.GONE);
		}

		private void setPendingView()
		{
			rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
			rotate.setDuration(600);
			rotate.setRepeatMode(Animation.RESTART);
			rotate.setRepeatCount(Animation.INFINITE);

			pendingView = LayoutInflater.from(AnswersActivity.this).inflate(R.layout.endless_row, null);
			View child = pendingView.findViewById(android.R.id.text1);
			child.setVisibility(View.GONE);

			child = pendingView.findViewById(R.id.throbber);
			child.setVisibility(View.VISIBLE);
			child.startAnimation(rotate);
		}
	}
}
